package com.boscloud.service;

import com.boscloud.mapper.UserMapper;
import com.boscloud.pojo.AppVersion;
import com.boscloud.pojo.User;
import io.jsonwebtoken.Jwt;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
   @Autowired
    private  UserMapper UserMapper;

    public List<User> getUserList(){
        return  UserMapper.getUserList();
    }

    public User login(User user){
        return UserMapper.getUserByUsernameAndPassword(user);
    }
    public String getJwt(User user){
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",user.getId().toString());
        claims.put("username",user.getUsername());
        claims.put("plant",user.getPlant());

        return Jwts.builder()
                .signWith(SignatureAlgorithm.HS256,"boscloud")
                .setClaims(claims)
                .setExpiration((new Date(System.currentTimeMillis() + 3600 * 1000)))
                .compact();
    }
    public int changePwd(User user){
           return UserMapper.updatePwd(user);
    }
    public AppVersion getVersion(){
        return UserMapper.getVersion();
    }
}
